package admin

import (
	"github.com/gin-gonic/gin"
	"jwgin/model/mysql"
	"jwgin/service"
)

//用户金豆变更
type recordBean struct {
}

func (l *recordBean) Routers(Router *gin.RouterGroup) (R gin.IRoutes) {
	Router.GET("record/bean", l.get)
	Router.GET("record/bean/user", l.getUser)
	Router.GET("record/bean/com", l.getCom)
	return Router
}

//获取所有列表
func (recordBean) get(c *gin.Context) {
	count, size, offset := pages(c)

	db := service.G.Model(mysql.RecordBean{}).
		Joins("JOIN `user` ON user.id = record_bean.uid").
		Joins("JOIN `company` ON company.id = user.comid")

	//按公司查找
	if companyid := c.Query("comid"); companyid != "" {
		db = db.Where("company.id = ?", companyid)
	}

	if uid := c.Query("uid"); uid != "" {
		db = db.Where("uid = ?", uid)
	}

	if tp := c.Query("type"); tp != "" {
		db = db.Where("type = ?", tp)
	}

	//时间筛选
	startTime := c.Query("startTime")
	endTime := c.Query("endTime")
	if startTime != "" && endTime != "" {
		db = db.Where(" record_bean.addtime > ? AND record_bean.addtime < ? ", startTime, endTime)
	}

	list := []map[string]interface{}{}
	db.Select("title,user.account,record_bean.*").Order("id DESC").Count(&count).Offset(offset).Limit(size).Scan(&list)
	RespList(c, count, list)
}
func (recordBean) getUser(c *gin.Context) {
	RespSucc(c, new(mysql.User).GetAll())
}
func (recordBean) getCom(c *gin.Context) {
	RespSucc(c, new(mysql.Company).GetAll())
}
